package com.gxc.mall.goods.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxc.mall.goods.config.TestRabbitQueueConfigBean;
import com.gxc.mall.goods.dto.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author GongXincheng
 * @since 2019-09-09 11:07
 */
@Slf4j
@Component
public class TestMqProvider {

    @Resource
    private AmqpTemplate amqpTemplate;

    @Resource
    private RabbitTemplate rabbitTemplate;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public void sendMessageObject() throws JsonProcessingException {
        UserEntity userEntity = new UserEntity();
        userEntity.setAge(23);
        userEntity.setUsername("GXC");
        userEntity.setPassword("123qwe");
        userEntity.setEmail("gxc@163.com");
        userEntity.setLastLoginTime(LocalDateTime.now());
        userEntity.setStatus(1);

        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.convertAndSend(TestRabbitQueueConfigBean.TEST_MESSAGE_OBJECT_QUEUE_NAME, userEntity);
    }

}
